<?php

namespace App\Hospital\Controllers;

use App\Admin\Forms\DataUploadForm;
use App\Admin\Forms\NoticeForm;
use App\Admin\Grids\HospitalUploadDataGrid;
use App\Hospital\Metrics\Examples;
use App\Http\Controllers\Controller;
use App\Jobs\HospitalUpload;
use App\Models\EbHospitalUploadData;
use App\Models\HospitalAdminUserHospital;
use App\Models\HospitalUploadData;
use Dcat\Admin\Admin;
use Dcat\Admin\Http\Controllers\Dashboard;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Widgets\Alert;
use Dcat\Admin\Widgets\Card;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class DataUploadController extends AdminController
{
    public function index(Content $content)
    {
        return $content
            ->header('数据同步')
            ->title('数据同步')
            ->description('数据同步')
            ->body(function (Row $row) {
                $row->column(4,function (Column $column){
                    $card = new Card(new DataUploadForm());
                    $card->noPadding();
                    $column->row($card);
                });
                $row->column(8, function (Column $column) {
                    $card = new Card(HospitalUploadDataGrid::make(['hospital_id'=>Admin::user()->hospital_id]));
                    $card->noPadding();
                    $column->row($card);
                });
            });
    }

    public function callback(Request $request)
    {
        $file = $request->input('file');
        list($hospital_id,$file_name) = explode('/',$file);
        $hospitalUpload = new HospitalUploadData();
        $hospitalUpload->hospital_id = $hospital_id;
        $hospitalUpload->file_name = $file_name;
        $hospitalUpload->save();
        HospitalUpload::dispatch($hospitalUpload);
        return new JsonResponse(['code'=>200,'msg'=>'成功'],200);
    }
}
